<?php

class Painel_UsuariosController extends App_Controller_Action
{
    private $_table     = 'USUARIOS';
    private $_pk        = 'USU_ID';
    private $_order_col = 'USU_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'USU_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'USU_FILE'; 
    public  $dir = 'public/upload/usuarios/';
    public  $db = '';
        
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Painel_Form_Usuarios();
        
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        
        $where = ' WHERE u.USU_TPO = 1';
        
        if($this->_PER_ID > 1){ // <> de Web mastar
            $where .= ' and u.PER_ID > 1'; // usuario logado
        }
        
        if($this->_PER_ID > 5){ // <> de administrador
            $where .= ' and u.USU_ID = '.$this->_USU_ID; // usuario logado
        }
        
        
        
        //$USU_STA = $this->_getParam('USU_STA', 1);
        //$where .= ' and USU_STA = "'.$USU_STA.'"';
        
        $dscfiltro = $this->_getParam('dscfiltro', '');
        if($dscfiltro > '') $where .= ' and (u.USU_NOM like "%'. $dscfiltro.'%")';
        $this->view->dscfiltro = $dscfiltro;
        
        $query = 'SELECT SQL_CACHE u.*, t.TUS_NOM, p.PER_NOM '
                .' FROM '.$this->_table.' u, TIPO_USUARIO t, PERMISSAO p '
                .' '.$where.' '
                .' AND u.TUS_ID = t.TUS_ID AND u.PER_ID = p.PER_ID '
                .' ORDER BY u.'.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_pdr.js');
        
        
        $this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue('USU_ID');
                    
                    $data = $this->form->getValues();
                    
                    unset($data['br']);
                    unset($data['USU_SNH']);
                    
                    if(strlen($this->form->getValue('USU_SNH')) > 1){
                        $data['USU_SNH'] = sha1($this->form->getValue('USU_SNH'));
                    }
                    
                    $this->db->beginTransaction();
                    if($id == 0){
                        
                        $data['USU_DATCAD'] = date('Y-m-d');
                        $data['USU_TPO'] = 1;
                        $data['USU_CADSTA'] = 1;
                        
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();   
                    }
                    else $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);
                    
                    $this->db->commit();

                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                //Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
                $this->db->rollBack();
            }
        } else {
            $id = $this->_getParam('id', 0);
            
            if ($id > 0) {
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $this->form->populate($data);
                            
                if($this->_PER_ID > 1){
                    if($data['PER_ID'] == 1){
                        $this->_helper->redirector('index');
                    }
                }

                if($this->_PER_ID > 5){
                    if($this->_USU_ID <> $data['USU_ID']){
                        $this->_helper->redirector('index');
                    }
                }

            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = '';
        $id = $this->_getParam('id', 0);
        if((int) $id > 0 ) {   
            $this->db->beginTransaction();
            try {
                $ok = (bool) $this->db->delete($this->_table, $this->_pk . ' = ' . $id);
                $this->db->commit();
            } catch(Exception $ex) {
                //echo getMessage();
                $ok = FALSE;
                $msg = MSG_DELETE;
                $this->db->rollBack();
            }          
        }     
        echo json_encode(array('ok' => $ok, 'msg'=> $msg));
    }

}